package com.jike.shop.cloud.service.strategy;

import com.google.common.base.Preconditions;
import com.jike.shop.cloud.entity.OrderInfo;
import com.jike.shop.cloud.enums.OrderPayMethodEnum;
import com.jike.shop.cloud.utils.ApplicationContextUtil;
import org.springframework.context.ApplicationContext;

public class PayContext {

    public static void pay(OrderPayMethodEnum orderPayMethodEnum, OrderInfo orderInfo, String transcationId){
        PayStrategy strategy = getStrategy(orderPayMethodEnum);
        strategy.pay(orderInfo, transcationId);
    }


    public static PayStrategy getStrategy(OrderPayMethodEnum orderPayMethodEnum) {
        ApplicationContext applicationContext = ApplicationContextUtil.getApplicationContext();
        String strategyName = "syncPay";
        if(!orderPayMethodEnum.isSyncPay()){
            strategyName = "asyncPay";

        }
        PayStrategy bean = applicationContext.getBean(strategyName, PayStrategy.class);
        Preconditions.checkNotNull(bean, "不支持策略【%s】", strategyName);
        return bean;
    }
}
